Skip to content

[patch] Spell NaN and infinity in each target language, with the import it needs - #147

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/non-finite-double-literals-96
Oct 5, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/non-finite-double-literals-96

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #96
Fixes #78

Summary

A non-finite double literal used to be written as its round-trip text. Every target except JavaScript got an undefined name (NaN, Infinity), and C# got the tokens NaNd / Infinityd. This implements the maintainer's decision from 2026-09-28 (option 1 on #78, with #96's table as the spec): each generator spells these values itself and adds the import its spelling needs.

Target NaN +∞ / −∞ Import added
C# double.NaN double.PositiveInfinity / double.NegativeInfinity none
C++ std::numeric_limits<double>::quiet_NaN() std::numeric_limits<double>::infinity() (negated) <limits>
C NAN INFINITY / -INFINITY <math.h>
Go math.NaN() math.Inf(1) / math.Inf(-1) "math"
Rust f64::NAN f64::INFINITY / f64::NEG_INFINITY none
Python float("nan") float("inf") / float("-inf") none
JavaScript NaN Infinity / -Infinity none (unchanged)

Changes

  • LanguageGeneratorBase
    • New virtual SpellNonFiniteDouble(double). Its default is the JavaScript spelling.
    • TryGenerateCommonNode sends non-finite doubles to it.
    • The reflection walk EnsureConserved used is now the shared SelfAndDescendants, and ContainsNonFiniteDouble(SourceFile) is built on it.
    • LeadingSign no longer treats NaN as negative. .NET's double.NaN has its sign bit set.
  • C, C++: override SpellNonFiniteDouble and RequiredImports. That reuses the existing required-imports mechanism Python already uses for enum, and it already de-duplicates against SourceFile.Imports.
  • Go: Go writes its import block in WriteFileDirectives. So ImportGroups adds "math" the same way it already adds "os" for entry points, and skips it when the file already imports it.
  • C#, Rust, Python: spelling only, since none of them needs an import.

Tests

New Coder.Test/Languages/NonFiniteDoubleLiteralTests.cs:

With the fix reverted, 25 of the 34 new test cases fail. The 9 that still pass are the JavaScript spellings, the no-import cases, and the cases where the file already carried the import. The full suite passes: 1052/1052.

🤖 Generated with Claude Code

https://claude.ai/code/session_015zULxdhphqscknnLMm1Bk1


Generated by Claude Code

…rt it needs

A non-finite double literal was written as its round-trip text, so every
target but JavaScript got an undefined name (NaN, Infinity) and C# got the
tokens NaNd and Infinityd. Each generator now spells these as its own
constant: double.NaN / double.PositiveInfinity in C#, std::numeric_limits
in C++, NAN / INFINITY in C, math.NaN() / math.Inf(±1) in Go, f64::NAN /
f64::INFINITY / f64::NEG_INFINITY in Rust, and float("nan") / float("inf")
in Python.

A file holding one now gets the include or import that spelling needs:
<math.h> for C, <limits> for C++, and "math" for Go. It goes through the
existing RequiredImports hook, so an import the file already carries is not
written twice. The reflection walk EnsureConserved used is now the shared
SelfAndDescendants, which is how a generator finds such a literal.

Fixes #78
Fixes #96

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015zULxdhphqscknnLMm1Bk1
@sonarqubecloud

sonarqubecloud Bot commented Oct 5, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants